/**
 * Created by jinxs on 14-6-25.
 * 依赖
 * ui.core.js
 * ui.position.js
 * ui.panel.js
 */
(function ($) {
    /**
     * @class 消息提示组件
     * @constructor uiMessagetip
     * @type {{show: Function}}
     */
    $.uiMessagetip = {
        show: function (options, param) {
            options = options || {};
            options = $.extend({}, $.uiMessagetip.defaults, options);
            options.title = options.title || '提示';
            _show(options);
        }
    };
    /**
     * 默认配置
     * @lends uiMessagetip.prototype
     */
    $.uiMessagetip.defaults = {
        /**
         * @type String
         * @default ui_panel_primary
         * @summary 指示组件的面板风格
         */
        panelStyle: 'ui_panel_primary',
        showHeader: true,
        /**
         * @type String
         * @default ""
         * @summary 指示组件的标题
         */
        title: '',
        /**
         * @type String
         * @default ""
         * @summary 指示组件的内容
         */
        content: '',
        /**
         * @type Number
         * @default 200
         * @summary 指示组件的宽度
         */
        width: 200,
        /**
         * @type Number
         * @default 100
         * @summary 指示组件的高度
         */
        height: 120,
        /**
         * @type Number
         * @default 0
         * @summary 与容器之间的间隔
         */
        padding: 0,
        /**
         * @type Boolean
         * @default true
         * @summary 指示组件是否有动画效果
         */
        animate: true,
        /**
         * @type Boolean
         * @default true
         * @summary 指示组件是否固定
         */
        fixed: true,
        /**
         * @type Object
         * @default $(document.body)
         * @summary 指示组件的父容器
         */
        container: $(document.body),
        /**
         * @type String
         * @default bottom
         * @summary 指示组件的垂直位置
         */
        vertical: 'bottom',
        /**
         * @type String
         * @default right
         * @summary 指示组件的水平位置
         */
        horizontal: 'right',
        /**
         * @type Number
         * @default 3000
         * @summary 指示组件的延时关闭时间
         */
        timeout: 3000,
        /**
         * @event
         * @name uiMessagetip#onClose
         * @type function
         * @default function () { }
         * @summary 指示组件的关闭事件
         */
        onClose: function () {
        }
        , zIndex: 99999
    }
    var _close = function (messagebox, handler) {
        handler ? handler() : jQuery.noop();
        messagebox.remove();
    };
    var _show = function (options) {
        var onClose = options.onClose;
        var messageBox = $('<div></div>').appendTo(options.container).hide();
        var msgboxWidth = options.width || 200;
        var msgboxHeight = options.height || 120;

        var cont='<div class="ui_messagetip_content';
        if(!options.showHeader){
            cont+=' ui_messagetip_content_noHead';
        }
        cont+='">' + options.content + '</div>';
        if ($.fn.uiPanel) {
            messageBox.uiPanel({
                zIndex: options.zIndex,
                icon: 'messagebox',
                width: msgboxWidth,
                height: msgboxHeight,
                closable: true,
                showHeader: options.showHeader,
                title: options.title,
                content: cont,
                onClose: function () {
                    _close(messageBox, onClose);
                }
            });
        }
        messageBox.addClass(options.panelStyle + " ui_messagetip");
        if (!options.showHeader) {
            var divContent = messageBox.uiPanel("content");
            var btnClose = $('<a type="button" class="ui_messagetip_close">' +
                '<span class="icon icon_close"></span></a>')
                .setIDByParentID(messageBox[0].id, $.uiTemplate.panel.btnClose).appendTo(divContent);
            btnClose.bind("click", function () {
                messageBox.uiPanel("close");
            });
            switch (options.panelStyle) {
                case "ui_panel_primary":
                    divContent.addClass("content_primary");
                    break;
                case "ui_panel_success":
                    divContent.addClass("content_success");
                    break;
                case "ui_panel_info":
                    divContent.addClass("content_info");
                    break;
                case "ui_panel_warning":
                    divContent.addClass("content_warning");
                    break;
                case "ui_panel_danger":
                    divContent.addClass("content_danger");
                    break;
            }
        }
        messageBox
            .bind("keydown.messagebox", function (event) {
                if (event.which == $.keyCode.ESCAPE) {
                    _close(messageBox, onClose);
                }
            }).hide();
        if (!options.animate || (_IE && _IEunder6)) {
            messageBox.uiPosition({
                horizontal: options.horizontal,
                vertical: options.vertical,
                container: options.container,
                fixed: options.fixed
            }).show();
        } else {
            switch (options.vertical) {
                case 'top':
                    messageBox.uiPosition({
                        horizontal: options.horizontal,
                        vertical: options.vertical,
                        container: options.container,
                        fixed: options.fixed
                    }).show();
                    var top = parseInt(messageBox.css('top')) + options.padding;
                    messageBox.css({top: top - messageBox.height(), opacity: 0});
                    if(options.horizontal=="left"){
                        messageBox.animate({top: top,left:options.padding, opacity: 1}, 600);
                    }else{
                        messageBox.animate({top: top,right:options.padding, opacity: 1}, 600);
                    }
                    break;
                case 'center':
                    messageBox.uiPosition({
                        horizontal: options.horizontal,
                        vertical: options.vertical,
                        container: options.container,
                        fixed: options.fixed
                    }).fadeIn();
                    break;
                case 'bottom':
                    messageBox.uiPosition({
                        horizontal: options.horizontal,
                        vertical: options.vertical,
                        container: options.container,
                        fixed: options.fixed
                    }).show();
                    var top = parseInt(messageBox.css('top')) - options.padding;
                    messageBox.css({top: top + messageBox.height(), opacity: 0});
                    if(options.horizontal=="left"){
                        messageBox.animate({top: top,left:options.padding, opacity: 1}, 600);
                    }else{
                        messageBox.animate({top: top,right:options.padding, opacity: 1}, 600);
                    }

                    break;
            }
        }

        var timer;

        function timeout(time) {
            timer = setInterval(function () {
                if (!options.animate || (_IE && _IEunder6)) {
                    _close(messageBox, onClose);
                } else {
                    switch (options.vertical) {
                        case 'top':
                            var tmp_top = parseInt(messageBox.css('top'));
                            messageBox.animate({top: tmp_top - messageBox.height(), opacity: 0}, function () {
                                _close(messageBox, onClose);
                            });
                            break;
                        case 'center':
                            messageBox.fadeOut(function () {
                                _close(messageBox, onClose);
                            });
                            break;
                        case 'bottom':
                            var tmp_top = parseInt(messageBox.css('top'));
                            messageBox.animate({top: tmp_top + messageBox.height(), opacity: 0}, function () {
                                _close(messageBox, onClose);
                            });
                            break;
                    }
                }
                clearInterval(timer);
            }, time);
        }

        if (options.timeout > 0) { //定时关闭
            timeout(options.timeout);
        }

        messageBox.bind('mouseover', function () {
            clearInterval(timer);
        }).bind('mouseout', function () {
            if (timer) {
                timeout(options.timeout);
            }
        });
        $(window).bind("resize", function () {
            if (messageBox.width > 0 && messageBox.height > 0) {
                messageBox.uiPosition({
                    horizontal: options.horizontal,
                    vertical: options.vertical,
                    container: options.container,
                    fixed: options.fixed
                });
            }
        });
    };

})(jQuery);
